/**
 * Company
 * Copyright (C) 2004-2018 All Rights Reserved.
 */
package com.idefav.springdemo2.aop.annonations.interceptors;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

/**
 * @author wuzishu
 * @version $Id MyInterceptor.java, v 0.1 2018-07-22 下午4:07 wuzishu Exp $$
 */

@Component
@Aspect
public class MyInterceptor {


    // 定义切入点
    @Pointcut("execution(* *.getPerson(..))")
    private void pointCut(){}

    @Before("pointCut())")
    public void before(JoinPoint joinPoint){

        System.out.println("前置通知");
    }

    @After("pointCut()")
    public void after(JoinPoint joinPoint){
        System.out.println("后置通知");
    }

    @AfterReturning("pointCut()")
    public void afterReturn(JoinPoint joinPoint){
        System.out.println("后置返回");
    }

    @AfterThrowing(value = "pointCut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Throwable ex){
        System.out.println("异常通知");
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint){

        Object result=null;
        System.out.println("环绕进入.");
        try {
            result=joinPoint.proceed();

            System.out.println("环绕返回");
        } catch (Throwable throwable) {
            System.out.println("环绕异常");
            throwable.printStackTrace();
        }
        return result;
    }

}
